<script setup lang='ts'>
  import { nextTick, reactive, ref } from 'vue';
  import { equipApi } from '@/api/controller';
  import { ToastSuccess, Toastloading, allNumber } from '@/utils/public';
  import xmUpload from '@/components/Upload/xm-upload.vue';
  const formLabelWidth = '130px'
  const dataForm = ref()
  const $emit = defineEmits(['updateList'])
  const action = reactive({
    dialogFormVisible:false,
    disabled:false,
    title:''
  })
  const options = [
    {value:0,label:'关闭'},
    {value:1,label:'开启'}
  ]
  interface formQuert {
    item_name:string
    item_img:string
    item_level:number
    sort:number
    if_screen:number
    if_notice:number
    base_rate:number
    low_rate:number
    high_rate:number
    new_protect_rate:number
    day_rate_1:number
    day_rate_2:number
  }
  let form = reactive({} as formQuert)
  const formRules = {
    item_name: [{ required: true, trigger: 'blur', message: '请输入一级礼物池名称' }],
    item_img: [{ required: true, trigger: 'blur', message: '请上传一级礼物池图片' }],
    item_level: [{ required: true, trigger: 'blur', message: '请输入一级礼物池稀有度' }],
    sort: [{ required: true, trigger: 'blur', message: '请输入排序' }],
    if_screen: [{ required: true, trigger: 'blur', message: '请选择是否飘屏' }],
    if_notice: [{ required: true, trigger: 'blur', message: '请选择是否页面公告' }],
    base_rate: [{ required: true, trigger: 'blur', message: '请输入基础概率(万分)' }],
    low_rate: [{ required: true, trigger: 'blur', message: '请输入低回概率' }],
    high_rate: [{ required: true, trigger: 'blur', message: '请输入高回概率' }],
    new_protect_rate: [{ required: true, trigger: 'blur', message: '请输入新人保护概率' }],
    day_rate_1: [{ required: true, trigger: 'blur', message: '请输入每日保护概率1' }],
    // day_rate_2: [{ required: true, trigger: 'blur', message: '请输入每日保护概率2' }],
  }
  const handleSubmit = ()=>{
    dataForm.value.validate(async(valid:any)=>{
      if(!valid) return
      const loading = Toastloading()
      equipApi.pollOneEdit(form).then(res=>{
        loading.close()
        $emit('updateList')
        ToastSuccess('配置修改成功')
        closeDialog()
      }).catch(()=>{
        loading.close()
      })

    })
  }
  const openDialog = (query:any = {})=> {
    const {
      item_name='', id, item_img, item_level, sort,if_screen,
      if_notice, base_rate, low_rate, high_rate,
      new_protect_rate, day_rate_1, day_rate_2
    } = query
    action.title = `编辑${item_name}礼物池`
    Object.assign(form,{
      item_name, id, item_img, item_level, sort,if_screen,
      if_notice, base_rate, low_rate, high_rate,
      new_protect_rate, day_rate_1, day_rate_2
    })
    action.dialogFormVisible = true
  }
  const closeDialog = ()=>{
    form = reactive({} as formQuert)
    nextTick(() => {
      dataForm.value.clearValidate();
    });
    action.dialogFormVisible = false
  }
  defineExpose({
    openDialog
  })
</script>

<template>
  <div>
    <el-dialog
    top="5vh"
    class="xm-dialog input-width"
    v-model="action.dialogFormVisible"
    :close-on-click-modal="action.disabled"
    :show-close="action.disabled"
    :close-on-press-escape="action.disabled"
    width="55%"
    :title="action.title">
      <el-form ref="dataForm" :model="form" :rules="formRules">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item :label-width="formLabelWidth" label="一级礼物池名称" prop="item_name">
              <el-input v-model="form.item_name" placeholder="请输入一级礼物池名称" />
            </el-form-item>
            <el-form-item :label-width="formLabelWidth" label="一级礼物池稀有度" prop="item_level">
              <el-input v-model="form.item_level" @input="allNumber(form,'item_level')" placeholder="请输入一级礼物池稀有度" />
            </el-form-item>
            <el-form-item :label-width="formLabelWidth" label="一级礼物池图片" prop="item_img">
              <xmUpload listType="picture-card" :data="{type:27}" v-model:file-url="form.item_img"/>
            </el-form-item>
            <el-form-item :label-width="formLabelWidth" label="飘屏公告状态" prop="if_screen">
              <el-select v-model="form.if_screen" >
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
            <el-form-item :label-width="formLabelWidth" label="页面公告状态" prop="if_notice">
              <el-select v-model="form.if_notice" >
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label-width="formLabelWidth" label="排序" prop="sort">
              <el-input v-model="form.sort" @input="allNumber(form,'sort')" placeholder="请输入排序" />
            </el-form-item>
            <el-form-item :label-width="formLabelWidth" label="基础概率(万分)" prop="base_rate">
              <el-input v-model="form.base_rate" @input="allNumber(form,'base_rate')" placeholder="请输入基础概率(万分)" />
            </el-form-item>
            <el-form-item :label-width="formLabelWidth" label="低回概率" prop="low_rate">
              <el-input v-model="form.low_rate" @input="allNumber(form,'low_rate')" placeholder="请输入低回概率" />
            </el-form-item>
            <el-form-item :label-width="formLabelWidth" label="高回概率" prop="high_rate">
              <el-input v-model="form.high_rate" @input="allNumber(form,'high_rate')" placeholder="高回概率" />
            </el-form-item>
            <el-form-item :label-width="formLabelWidth" label="新人保护概率" prop="new_protect_rate">
              <el-input v-model="form.new_protect_rate" @input="allNumber(form,'new_protect_rate')" placeholder="请输入新人保护概率" />
            </el-form-item>
            <el-form-item :label-width="formLabelWidth" label="每日保护概率1" prop="day_rate_1">
              <el-input v-model="form.day_rate_1" @input="allNumber(form,'day_rate_1')" placeholder="请输入每日保护概率1" />
            </el-form-item>
            <!-- <el-form-item :label-width="formLabelWidth" label="每日保护概率2" prop="day_rate_2">
              <el-input v-model="form.day_rate_2" @input="allNumber(form,'day_rate_2')" placeholder="请输入每日保护概率2" />
            </el-form-item> -->
          </el-col>
        </el-row>

      </el-form>

      <template v-if="!action.disabled" #footer>
        <span class="dialog-footer">
          <el-button @click="closeDialog">取 消</el-button>
          <el-button type="primary" @click="handleSubmit()">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
